package ps.client.sound;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JFrame;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.Player;


public class SoundPlayer {
    
    static boolean mute = false;
    
    
    public static void playMp3(InputStream in, float volume) throws JavaLayerException, IOException {
        FactoryRegistry r = FactoryRegistry.systemRegistry();
//        AudioDevice audio = r.createAudioDevice();
//        Player player = new Player(in, audio);
        Player player = new Player(in);
//        if (audio instanceof JavaSoundAudioDevice) {
//            JavaSoundAudioDevice jsAudio = (JavaSoundAudioDevice) audio;
//            jsAudio.setVolume(volume);
//        }
        player.play();
    }
    
    public static void playMp3Ansynchron(InputStream in, float volume) throws JavaLayerException, IOException {
        Thread mp3Thread = new Thread(new Mp3Runnable(in, volume), "PlayMp3Thread");
        mp3Thread.setDaemon(true);
        mp3Thread.start();
    }
    
    public static void playMp3(String file, float volume) throws JavaLayerException, IOException {
        playMp3(new FileInputStream(file), volume);
    }
    
    public static void playMp3Ansynchron(String file, float volume) throws JavaLayerException, IOException {
        playMp3Ansynchron(new FileInputStream(file), volume);
    }
    
    public static void playWav(InputStream in, float volume) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
        AudioInputStream stream = AudioSystem.getAudioInputStream(in);
        DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());
        Clip clip = (Clip)AudioSystem.getLine(info);
        clip.open(stream);
        clip.start();
    }
    
    public static void play(String file) throws FileNotFoundException, UnsupportedAudioFileException, IOException, LineUnavailableException, JavaLayerException {
        if (!mute) {
    //        System.out.println("PLAY: " + file);
            if (file.toUpperCase().endsWith(".WAV")) {
                playWav(new FileInputStream(file), 1f);
            } else if (file.toUpperCase().endsWith(".MP3")) {
                playMp3Ansynchron(new FileInputStream(file), 1f);
            }
        }
    }
    
    public static boolean isMute() {
        return mute;
    }

    
    public static void setMute(boolean mute) {
        SoundPlayer.mute = mute;
    }

    public static void main(String[] args) throws JavaLayerException, IOException, UnsupportedAudioFileException, LineUnavailableException {
//        SoundPlayer.playMp3Ansynchron(SoundPlayer.class.getResource("sword.mp3").openStream(), 1f);
//        SoundPlayer.playMp3(SoundPlayer.class.getResource("bell.mp3").openStream(), 1f);
        playWav(new FileInputStream("sounds/inc in 10.wav"), 1f);
        (new JFrame()).setVisible(true);
    }
    

}
